#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

int main (int argc, char *argv[])
{
	unsigned char *buffer = NULL;

	// Allocate the buffer needed
	buffer = (unsigned char *)malloc(1024);
	if (buffer == NULL) {
		return 0;
	}

	for (int i = 0; i < 1024; i++) {
		memset(buffer + i, i, 1);
	}

	int fd;
	char *name = (char*)"test.txt";
	int size = 1024;
	int i;
	static char buf[4] = {"01 "};

	// Open local file
	if ((fd = open(name, O_CREAT | O_WRONLY, 0)) == -1) {
		perror(name);
		return 0;
	}

	// Write data to the file
	for (i = 0; i < size; i++) {
		sprintf(buf, "%02x ", (unsigned int)((unsigned char)(*(buffer + i))));
		write(fd, &buf, sizeof(buf));
		printf("%02x ", (unsigned int)((unsigned char)(*(buffer + i))));
		if (!((i + 1) % 32)) {
			write(fd, (char*)"\n", 3);
			printf("\n");
		}
	}
	write(fd, (char*)"\n", 3);
	printf("\n");
	close(fd);
}
